package datamodels;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

/**
 * This will keep track of everything, the different conferences and more.
 * @author anthonysierra
 *
 */
public class GlobalDataStore 
{
	XStream xstream = new XStream(new StaxDriver());
	ArrayList<Conference> my_conferences = new ArrayList<Conference>();
	Conference my_conference;
	public GlobalDataStore()
	{
		setupXStream();
		try
		{
			fillData();	
		}
		catch(Exception e)
		{
			System.out.println("Issue");
		}
	}
	/**
	 * This will setup everything xstream needs for writing and reading data.
	 */
	private void setupXStream()
	{
		xstream.alias("conference", this.getClass());
		xstream.alias("manuscript", Manuscript.class);
		xstream.alias("role", Role.class);
		xstream.alias("review", Review.class);
		xstream.alias("User", User.class);
	}
	/**
	 * This fills the data store with information about a conference.
	 * @author Rithy Chhen
	 * @throws FileNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public void fillData() throws FileNotFoundException
	{
		StringBuilder text =
			new StringBuilder();
		//Get a scanner and depending on whether we are testing or not use the following files.
		Scanner scanner = new Scanner(new FileInputStream("src/datamodels/conferenceinfo.txt"), "UTF-8");
		try {	
			while (scanner.hasNextLine()){
				text.append(scanner.nextLine());
			}
		}
		finally{
			scanner.close();
		}
		//Fills the array with conferences that exist.
		my_conferences = (ArrayList<Conference>)xstream.fromXML(text.toString());
	}
	public void setConference(Conference the_conference)
	{
		my_conference = the_conference;
	}
	/**
	 * This will write the data about the conference into the arraylist and then write the arraylist into the file.
	 * @throws IOException
	 */
	public void writeData() throws IOException
	{
		String data = xstream.toXML(my_conferences);
		//System.out.println(data);
		Writer out = new OutputStreamWriter(new FileOutputStream("src/datamodels/conferenceinfo.txt"), "UTF-8");
	    try {
	        out.write(data);
	      }
	      finally {
	        out.close();
	      }	
	}
	public Conference getCurrentConference()
	{
		return my_conference;
	}
	public List<Conference> getConferences()
	{
		return my_conferences;
	}
	/**
	 * This will add a conference object to the global data store for future writing.
	 * @param the_conference
	 */
	public void addConferenceObject(Conference the_conference)
	{
		for (int i = 0; i < my_conferences.size(); i++)
		{
			if (my_conferences.get(i).getName().equals(the_conference.getName()))
			{
				my_conferences.remove(i);
				break;
			}
		}
		my_conferences.add(the_conference);
		try
		{
			writeData();	
		}
		catch(Exception e)
		{
			System.out.println("Couldnt write the addition of the conference");
		}
	}
	
	/**
	 * Delete a conference from the store.
	 * @param the_conference
	 */
	public void removeConferenceObject(Conference the_conference)
	{
		if (my_conferences.contains(the_conference)) {
			my_conferences.remove(the_conference);
			try {
				writeData();
			} catch (IOException e) { }
		}
	}
	
	/**
	 * Return the count of conferences in existence in the system.
	 * @return
	 */
	public int getConferenceCount()
	{
		return my_conferences.size();
	}
}
